package com.eascs.admin.classify.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.classify.dao.O2oClassifyDao;
import com.eascs.admin.classify.entity.O2oLoanAbjustForm;
import com.eascs.admin.classify.entity.O2oLoanAbjustType;
import com.eascs.admin.classify.entity.O2oLoanAbjustTypeFlow;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.app.dao.PageResult;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Service
public class O2oClassifySerivce implements IO2oClassifyService {
	@Autowired
	O2oClassifyDao classifyDao;

	@Override
	public int insert(O2oLoanAbjustType abjustType) {
		// TODO Auto-generated method stub
		return classifyDao.insert(abjustType);
	}
	
	public int insertFlow(O2oLoanAbjustTypeFlow abjustType) {
		// TODO Auto-generated method stub
		return classifyDao.insert(abjustType);
	}

	@Override
	public int update(O2oLoanAbjustType abjustType) {
		// TODO Auto-generated method stub
		return classifyDao.updateById(abjustType);
	}
	
	public int updateFlow(O2oLoanAbjustTypeFlow abjustType) {
		// TODO Auto-generated method stub
		return classifyDao.updateById(abjustType);
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return classifyDao.deleteById(O2oLoanAbjustType.class, id);
	}

	@Override
	public O2oLoanAbjustType queryById(String id) {
		// TODO Auto-generated method stub
		return classifyDao.getById(O2oLoanAbjustType.class, id);
	}
	
	/**
	 * 根据流程id查询流程
	 * @param id
	 * @return
	 */
	public O2oLoanAbjustTypeFlow queryFlowById(String id) {
		// TODO Auto-generated method stub
		return classifyDao.getById(O2oLoanAbjustTypeFlow.class, id);
	}
	/**
	 * 根据自动分类表id查询数据最新的一条
	 * @param id
	 * @return
	 */
	public O2oLoanAbjustTypeFlow queryFlowCById(String id) {
		// TODO Auto-generated method stub
		String sql = "select olat.* from o2o_loan_abjust_type_flow olat where olat.abjust_id=? and is_new='Y' ";
		return classifyDao.findEntity(sql, O2oLoanAbjustTypeFlow.class, id);
	}
	
	public O2oLoanAbjustType queryByLoanapplyNo(String loanapplyNo) {
		// TODO Auto-generated method stub
		String sql = "select * from o2o_loan_abjust_type where loanapply_no=?";
		return classifyDao.findEntity(sql, O2oLoanAbjustType.class, loanapplyNo);
	}

	@Override
	public PageResult<Object[]> queryByPageAuto(O2oLoanAbjustForm abjustForm) {
		return classifyDao.queryByPageAuto(abjustForm);
	}
	
	public PageResult<Object[]> queryByPageAuto(O2oLoanAbjustForm abjustForm ,String download) {
		return classifyDao.queryByPageAuto(abjustForm ,download);
	}

	@Override
	public PageResult<Object[]> queryByPageMamuil(O2oLoanAbjustForm abjustForm) {
		return classifyDao.queryByPageMamuil(abjustForm);
	}
	
	public List<Object[]> queryList(String billCode){
		String sql = "select olat.loanapply_no,olat.bill_code,olat.loan_user_name,"
				+ "case when olat.new_loan_type='NORMAL_LOAN' then '正常贷款'"
				+ " when olat.new_loan_type='BAD_LOAN' then '不良贷款' when olat.new_loan_type='FOLLOW_LOAN' then '关注贷款' end "
				+ " from o2o_loan_abjust_type olat where  olat.bill_code like '%"+billCode+"%' limit 0,10";
		return classifyDao.findObjectsList(sql);
	}

	@Override
	public List<O2oLoanAbjustType> queryMAList() {
		List<O2oLoanAbjustType> abjustTypes = new ArrayList<O2oLoanAbjustType>();
		String sql = "select * from o2o_loan_abjust_type where data_type='MA'";
		abjustTypes = classifyDao.findEntitys(sql, O2oLoanAbjustType.class);
		return abjustTypes;
	}

	@Override
	public List<O2oLoanAbjustType> queryAUList() {
		List<O2oLoanAbjustType> abjustTypes = new ArrayList<O2oLoanAbjustType>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT oll.loanapply_no,oll.bill_code,oll.ea_mount as loan_amount,oll.ltd,oll.ltd_code,oll.ltd_name");
		sql.append(",olu.loan_id as loan_user_no,olu.user_full_name as loan_user_name,oll.operator_name as operer_name");
		sql.append(",opr.province_code,opr.province_name,oeld.pay_amount,oeld.payment_amount,oor.defaultamount,oll.wf_type as data_type");
		sql.append(" from o2o_loanapply oll LEFT JOIN o2o_loan_user olu on oll.loanapply_no = olu.loanapply_no and oll.loan_id = olu.loan_id");
		sql.append(" left join  o2o_bank_loan_data loan on oll.bill_code =loan.bill_code ");
		sql.append("LEFT JOIN o2o_platform_regional opr on oll.ltd = opr.platform_id ");
		sql.append("	LEFT JOIN o2o_ea_loan_data oeld on oeld.bill_code_ea = oll.bill_code and oeld.loan_type='DC'");
		sql.append(" LEFT JOIN o2o_overdue_report oor on oor.loanapplyno=oll.loanapply_no");
		sql.append(" where oll.loan_flg='Y' and loan.is_del='N' and oll.wf_type <> 'HN' and olu.sponsor_type is null and olu.spouse_id is null and oll.bill_code not in (''");
		List<O2oLoanAbjustType> queryMAList = this.queryMAList();
		if (queryMAList != null && queryMAList.size() > 0) {
			for (O2oLoanAbjustType abjustType : queryMAList) {
				sql.append(",'" + abjustType.getBillCode()+"'");
			}
		}
		sql.append(") GROUP BY oll.bill_code ORDER BY oll.payment_time desc ");
		abjustTypes = classifyDao.findEntitys(sql.toString(), O2oLoanAbjustType.class);
		return abjustTypes;
	}

	@Override
	public int insertAU(O2oLoanAbjustType abjustType) {
		String sql = "delete from o2o_loan_abjust_type where data_type=? ";
		classifyDao.execute(sql, "AU");
		int i = 0;
		User user = null;
		if (null == UserHolder.getUser()) {
			user = new User();
			user.setNo("1");
			user.setCname("系统管理员");
		} else {
			user = UserHolder.getUser();
		}
		List<O2oLoanAbjustType> queryMAList = this.queryAUList();
		if (queryMAList != null && queryMAList.size() > 0) {
			for (O2oLoanAbjustType newabjustType : queryMAList) {
				abjustType = new O2oLoanAbjustType();
				abjustType.setDataType("AU");//自动改变类型
				abjustType.setIsDelete(StatusContexts.O2O_NO);
				abjustType.setCreateUserId(user.getNo());
				abjustType.setCreateTime(new Date());
				abjustType.setCreateUserName(user.getCname());
				if ("O".equals(newabjustType.getDataType())) {
					abjustType.setNewLoanType("NORMAL_LOAN");// 正常贷款
					abjustType.setOldLoanType("NORMAL_LOAN");// 正常贷款
				} else {
					abjustType.setNewLoanType("FOLLOW_LOAN");// 关注贷款
					abjustType.setOldLoanType("FOLLOW_LOAN");// 关注贷款
				}
				abjustType.setBillCode(newabjustType.getBillCode());
				abjustType.setLoanapplyNo(newabjustType.getLoanapplyNo());
				abjustType.setLoanUserNo(newabjustType.getLoanUserNo());
				abjustType.setLoanUserName(newabjustType.getLoanUserName());
				abjustType.setLoanAmount(newabjustType.getLoanAmount());
				abjustType.setPayAmount(newabjustType.getPayAmount());
				abjustType.setPaymentAmount(newabjustType.getPaymentAmount());
				abjustType.setOpererName(newabjustType.getOpererName());
				abjustType.setLtd(newabjustType.getLtd());
				abjustType.setLtdCode(newabjustType.getLtdCode());
				abjustType.setLtdName(newabjustType.getLtdName());
				abjustType.setProvinceCode(newabjustType.getProvinceCode());
				abjustType.setProvinceName(newabjustType.getProvinceName());
				abjustType.setDefaultamount(newabjustType.getDefaultamount());
				i = this.insert(abjustType);
			}
		}
		return i;
	}
	
}
